package cn.slipi.admin.controller;

import cn.slipi.admin.biz.PdrApiBiz;
import cn.slipi.admin.biz.vo.iface.CsmGrantVo;
import cn.slipi.admin.biz.vo.iface.GrantInfoVo;
import cn.slipi.admin.biz.vo.iface.PdrApiVo;
import cn.slipi.admin.common.annotation.Permission;
import cn.slipi.admin.common.dto.PageDTO;
import cn.slipi.admin.common.response.JsonResponse;
import cn.slipi.admin.constants.AdminConstants;
import cn.slipi.admin.constants.permission.PermissionConstants;
import cn.slipi.admin.controller.form.pdrapi.GrantListForm;
import cn.slipi.admin.controller.form.pdrapi.ListForm;
import cn.slipi.admin.controller.form.pdrapi.NoGrantListForm;
import cn.slipi.admin.controller.form.pdrapi.SaveForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 接口管理
 */
@Validated
@Controller
@ResponseBody
@RequestMapping(AdminConstants.CAR_API_PREFIX + "/interface")
public class PdrApiController {


    @Autowired
    private PdrApiBiz pdrApi;

    @PostMapping("/list")
    @Permission(value = {PermissionConstants.INTERFACE_LIST})
    public JsonResponse<PageDTO<PdrApiVo>> getList(@Valid @RequestBody ListForm listForm) {
        JsonResponse<PageDTO<PdrApiVo>> responseVo = new JsonResponse<>();
        PageDTO<PdrApiVo> result = this.pdrApi.getList(listForm);
        responseVo.setData(result);
        return responseVo;
    }

    @PostMapping("/no/grant/list")
    @Permission(value = {PermissionConstants.INTERFACE_GRANT_LIST})
    public JsonResponse<PageDTO<PdrApiVo>> getNoGrantList(@Valid @RequestBody NoGrantListForm noGrantListForm) {
        JsonResponse<PageDTO<PdrApiVo>> responseVo = new JsonResponse<>();
        PageDTO<PdrApiVo> result = this.pdrApi.getNoGrantList(noGrantListForm);
        responseVo.setData(result);
        return responseVo;
    }

    @PostMapping("/grant/list")
    @Permission(value = {PermissionConstants.INTERFACE_GRANT_LIST})
    public JsonResponse<PageDTO<CsmGrantVo>> getGrantList(@Valid @RequestBody GrantListForm grantListForm) {
        JsonResponse<PageDTO<CsmGrantVo>> responseVo = new JsonResponse<>();
        PageDTO<CsmGrantVo> result = this.pdrApi.getGrantList(grantListForm);
        responseVo.setData(result);
        return responseVo;
    }

    @PostMapping("/add")
    @Permission(value = {PermissionConstants.INTERFACE_ADD})
    public JsonResponse<PdrApiVo> add(@Valid @RequestBody SaveForm saveForm) {
        JsonResponse<PdrApiVo> responseVo = new JsonResponse<>();
        PdrApiVo result = this.pdrApi.add(saveForm);
        responseVo.setData(result);
        return responseVo;
    }

    @PutMapping("/update/{id}")
    @Permission(value = {PermissionConstants.INTERFACE_UPDATE})
    public JsonResponse<PdrApiVo> update(@PathVariable("id") Integer id, @Valid @RequestBody SaveForm saveForm) {
        JsonResponse<PdrApiVo> responseVo = new JsonResponse<>();
        PdrApiVo result = this.pdrApi.update(id, saveForm);
        responseVo.setData(result);
        return responseVo;
    }

    @GetMapping("/publish/{id}")
    @Permission(value = {PermissionConstants.INTERFACE_PUBLISH})
    public JsonResponse<Boolean> detail(@PathVariable("id") Integer id) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.pdrApi.publish(id);
        responseVo.setData(result);
        return responseVo;
    }

    @PostMapping("/publish/batch")
    @Permission(value = {PermissionConstants.INTERFACE_PUBLISH_BATCH})
    public JsonResponse<Boolean> publishBatch(@Valid @RequestBody List<Integer> apiIds) {
        JsonResponse<Boolean> responseVo = new JsonResponse<>();
        Boolean result = this.pdrApi.publishBatch(apiIds);
        responseVo.setData(result);
        return responseVo;
    }


    @GetMapping("/grant/info/{id}")
    @Permission(value = {PermissionConstants.INTERFACE_GRANT_INFO})
    public JsonResponse<List<GrantInfoVo>> grantInfo(@PathVariable("id") Integer id) {
        JsonResponse<List<GrantInfoVo>> responseVo = new JsonResponse<>();
        List<GrantInfoVo> result = this.pdrApi.grantInfo(id);
        responseVo.setData(result);
        return responseVo;
    }

}
